package com.starrk.myblog.foreground.templete;

import com.starrk.myblog.foreground.mapper.properties.RedisProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisCluster;

@Component
public class JedisTemplete {

    private static final Logger LOGGER = LoggerFactory.getLogger(JedisTemplete.class);

    private static final String KEY_SPLIT = ":";
    @Autowired
    private RedisProperties redisProperties;
    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 设置缓存
     * @param prefix
     * @param key
     * @param value
     */
    public void set(String prefix,String key,String value){
        if(StringUtils.isEmpty(prefix)){
            throw new IllegalArgumentException("前缀不能为空");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key值不能为空");
        }
        jedisCluster.set(prefix+KEY_SPLIT+key,value);
        LOGGER.debug("RedisUtil : set cache key={},value={}",prefix+KEY_SPLIT+key,value);
    }
    /**
     * 设置缓存
     * @param prefix
     * @param key
     */
    public String get(String prefix,String key){
        if(StringUtils.isEmpty(prefix)){
            throw new IllegalArgumentException("前缀不能为空");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key值不能为空");
        }
        String back = jedisCluster.get(prefix+KEY_SPLIT+key);
        LOGGER.debug("RedisUtil : get cache key={},value={}",prefix+KEY_SPLIT+key);
        return back;
    }

    /**
     * 设置缓存并设置过期时间。
     * @param prefix
     * @param key
     * @param value
     * @param expireTime
     */
    public void setWithExpiretime(String prefix,String key ,String value ,int expireTime){
        if(StringUtils.isEmpty(prefix)){
            throw new IllegalArgumentException("前缀不能为空");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key值不能为空");
        }
        jedisCluster.setex(prefix+KEY_SPLIT+key,expireTime,value);
        LOGGER.debug("RedisUtils : set cache key= {},value={} ,expireTime={}",prefix+KEY_SPLIT+key,value,expireTime);
    }

    /**
     * 通过配置文件获取过期时间并存储
     * @param prefix
     * @param key
     * @param value
     */
    public void setWithExpiretime(String prefix,String key,String value){
        if(StringUtils.isEmpty(prefix)){
            throw new IllegalArgumentException("前缀不能为空");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key值不能为空");
        }
        jedisCluster.setex(prefix+KEY_SPLIT+key,redisProperties.getExpireTime(),value);
        LOGGER.debug("RedisUtils : set cache key= {},value={} ,expireTime={}",prefix+KEY_SPLIT+key,value,redisProperties.getExpireTime());
    }

    public void deleteCacheByKey(String prefix,String key){
        if(StringUtils.isEmpty(prefix)){
            throw new IllegalArgumentException("前缀不能为空");
        }
        if(StringUtils.isEmpty(key)){
            throw new IllegalArgumentException("key值不能为空");
        }
        jedisCluster.del(prefix+KEY_SPLIT+key);
        LOGGER.debug("RedisUtils : Delete cache by key= {}",prefix+KEY_SPLIT+key);

    }




}
